#ifndef SHENLAN_FLOWMAP
#define SHENLAN_FLOWMAP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

//这个是针对FlowMapPainter工具做的解码
float2 DecodeFlowDir(float2 flowMap)
{
    //原式 = ((flowMap_ * 255) / 256) * 2 - 1;
    return flowMap * 1.9921875f - 1;
}

half4 ApplyDecodedFlowMap(float2 decodedFlowmap, Texture2D tex, SamplerState sam, float2 uv, float time, float strength, float speed)
{
    float p1 = frac(time * speed);
    float p2 = frac(time * speed + 0.5);

    float2 flowUV = strength * decodedFlowmap;
    half4 mainCol_1 = SAMPLE_TEXTURE2D(tex, sam, uv + p1 * flowUV);
    half4 mainCol_2 = SAMPLE_TEXTURE2D(tex, sam, uv + p2 * flowUV);
    float timeLerp = abs(p1 * 2 - 1);

    return lerp(mainCol_1, mainCol_2, timeLerp);
}

half4 ApplyFlowMap(float2 encodedFlowmap, Texture2D tex, SamplerState sam, float2 uv, float time, float strength, float speed)
{
    float2 flowDir = DecodeFlowDir(encodedFlowmap);
    return ApplyDecodedFlowMap(flowDir, tex, sam, uv, time, strength, speed);
}

half4 ApplyFlowMap(Texture2D flowMap, SamplerState flowMapSampler, Texture2D tex, SamplerState sam, float2 uv, float time, float strength, float speed)
{
    float2 encodedFlowmap = SAMPLE_TEXTURE2D(flowMap, flowMapSampler, uv).rg;
    return ApplyFlowMap(encodedFlowmap, tex, sam, uv, time, strength, speed);
}

#endif
